﻿#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct stu
{
	char name[20];
	int age;
};

int com(const void* p1, const void* p2)
{
	return strcmp(((struct stu*)p1)->name , ((struct stu*)p2)->name);
}

void test()
{
	struct stu arr[] = { {"zhangsan",20}, { "lisi",35 } ,{ "wangwu" ,25 } };
	int sz = sizeof(arr) / sizeof(arr[0]);
	qsort(arr, sz, sizeof(arr[0]), com);
}

int main()
{
	test();
	return 0;
}


//struct Stu //学⽣
//{
//	char name[20];//名字
//	int age;//年龄
//};
////假设按照年龄来⽐较
//int cmp_stu_by_age(const void* e1, const void* e2)
//{
//	return ((struct Stu*)e1)->age - ((struct Stu*)e2)->age;
//}
////strcmp - 是库函数，是专⻔⽤来⽐较两个字符串的⼤⼩的
////假设按照名字来⽐较
//int cmp_stu_by_name(const void* e1, const void* e2)
//{
//	return strcmp(((struct Stu*)e1)->name, ((struct Stu*)e2)->name);
//}
////按照年龄来排序
//void test2()
//{
//	struct Stu s[] = { {"zhangsan", 20}, {"lisi", 30}, {"wangwu", 15} };
//	int sz = sizeof(s) / sizeof(s[0]);
//	qsort(s, sz, sizeof(s[0]), cmp_stu_by_age);
//}
////按照名字来排序
//void test3()
//{
//	struct Stu s[] = { {"zhangsan", 20}, {"lisi", 30}, {"wangwu", 15} };
//	int sz = sizeof(s) / sizeof(s[0]);
//	qsort(s, sz, sizeof(s[0]), cmp_stu_by_name);
//}
//int main()
//
//
//{
//	test2();
//	test3();
//	return 0;
//}